home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 548 < prev    next >
Internet Message Format  |  1996-08-06  |  2KB

  1. Path: mips.complang.tuwien.ac.at!schwarz
  2. From: schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz)
  3. Newsgroups: comp.std.c
  4. Subject: Re: integral types in switch expressions
  5. Date: 12 Mar 1996 12:52:11 GMT
  6. Organization: TU Wien
  7. Message-ID: <4i3s1r$s4o@news.tuwien.ac.at>
  8. References: <DnKnzv.8t6%spenford@zoo.toronto.edu> <Dnt4vo.Gu0@tigadmin.ml.com>
  9. NNTP-Posting-Host: mips.complang.tuwien.ac.at
  10.  
  11. In article <Dnt4vo.Gu0@tigadmin.ml.com>,
  12. |> pardoej@lonnds.ml.com (Julian Pardoe LADS LDN X1428) writes:
  13. |> In article <DnKnzv.8t6%spenford@zoo.toronto.edu>,
  14. |> Henry Spencer <henry@zoo.toronto.edu>
  15. |> explains that a compiler might not be able to use an
  16. |> implementation clever than an
  17. |> if/elsif chain when compiling a switch on a pointer
  18. |> value and then goes on to say:
  19. |>  
  20. |> -->. . .                  When the values are not known at compile time,
  21. |> -->it's hard to implement the switch as anything other than a series of
  22. |> -->ifs... and there is no point in writing that as a switch.
  23. |> 
  24. |> Yes there is!  It might be clearer, more readable code.  The fact that the
  25. |> compiler has to implement it in a certain way is not really relevant.
  26. |> 
  27. |> This isn't a big deal in C because one can't really define new types with
  28. |> enum-like behaviour but I really miss a generalized switch in C++.  If I
  29. |> can switch on an enum why can't I switch on a value of some class that wraps
  30. |> up a simple value?  The fact that the compiler has to use an if/elsif chain rather
  31. |> than a jump table is its problem, not mine!
  32.  
  33. I'm glad someone else sees a need for a generalized switch statement.
  34. I'd be satisfied with the type of constant expression that can be used
  35. in intializers, i.e., a null pointer constant, an address constant, or
  36. an address constant plus or minus an integral constant expression, as
  37. defined in ANSI 6.4 Constant expressions, so that no changes to linkers
  38. are required.
  39.  
  40. Constants of type ``address constant plus or minus an integral
  41. constant expression'' are of course prime targets for jump tables, especially
  42. if the set of constant expressions constitute a range.  The one optimization
  43. you can't do is binary search for the correct label, since you can't order
  44. the labels at compile time.
  45.  
  46. Konrad Schwarz
  47.